local skel = fk.CreateSkill {
  name = "emo__yangsan",
}

local EMO = require "packages.moepack.utility.emo_util"

skel:addEffect(fk.Damaged, {
  anim_type = "masochism",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and player:getMark("@emo__xueyan") > 0
  end,
  on_cost = function (self, event, target, player, data)
    local x = math.min(4, player:getMark("@emo__xueyan"))
    local num = EMO.chooseNum(player, 1, x, "#emo__yangsan-cost")
    if num then
      event:setCostData(self, {num = num})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local num = event:getCostData(self).num
    room:removePlayerMark(player, "@emo__xueyan", num)
    room:addPlayerMark(player, "emo__yangsan_used-turn", num)
    local chosens = {}
    local all_choices = {"emo__yangsan_draw", "emo__yangsan_move", "emo__yangsan_nullify", "emo__yangsan_slash"}
    for i = 1, num do
      if player.dead then return end
      local choices = table.filter(all_choices, function (ch) return not table.contains(chosens, ch) end)
      local wound = player:getLostHp()
      if wound == 0 then table.removeOne(choices, "emo__yangsan_draw") end
      if #room:canMoveCardInBoard() == 0 then table.removeOne(choices, "emo__yangsan_move") end
      if player:prohibitUse(Fk:cloneCard("stab__slash")) then table.removeOne(choices, "emo__yangsan_slash") end
      if #choices == 0 then break end
      local choice = room:askToChoice(player, {
        choices = choices, all_choices = all_choices, skill_name = skel.name, prompt = "#emo__yangsan-choice",
      })
      table.insert(chosens, choice)
      if choice == "emo__yangsan_draw" then
        player:drawCards(wound, skel.name)
      elseif choice == "emo__yangsan_move" then
        local tos = room:askToChooseToMoveCardInBoard(player, {
          skill_name = skel.name, prompt = "#emo__yangsan-move",
        })
        if #tos == 2 then
          room:askToMoveCardInBoard(player, {
            skill_name = skel.name, target_one = tos[1], target_two = tos[2],
          })
        end
      elseif choice == "emo__yangsan_nullify" then
        local tos = room:askToChoosePlayers(player, {
          targets = room.alive_players, max_num = 1, min_num = 1, skill_name = skel.name, prompt = "#emo__yangsan-nullify",
        })
        if #tos == 1 then
          room:addPlayerMark(tos[1], MarkEnum.UncompulsoryInvalidity.."-turn", 1)
        end
      elseif choice == "emo__yangsan_slash" then
        room:askToUseVirtualCard(player, {
          name = "stab__slash", skill_name = skel.name, extra_data = {bypass_distances = true, bypass_times = true}
        })
      end
    end
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  if not (player:hasSkill("emo__xueyan", true) or player:hasSkill("emo__yangsan", true) or player:hasSkill("emo__luomu", true)) then
    player.room:setPlayerMark(player, "@emo__xueyan", 0)
  end
end)

Fk:loadTranslationTable{
  ["emo__yangsan"] = "阳伞",
  [":emo__yangsan"] = "当你受到伤害后，你可以消耗任意层血宴执行等量项：1.摸X张牌；2.移动场上的一张牌；3.令一名角色本回合非锁定技失效；4.视为使用一张刺【杀】（X为你已损失体力值）。",
  ["#emo__yangsan-choice"] = "阳伞：选择一项效果执行",
  ["#emo__yangsan-cost"] = "阳伞：你可以消耗数层“血宴”执行等量项效果",
  ["emo__yangsan_draw"] = "摸数张牌",
  ["emo__yangsan_move"] = "移动场上牌",
  ["emo__yangsan_nullify"] = "沉默一名角色",
  ["emo__yangsan_slash"] = "视为使用刺杀",
  ["#emo__yangsan-nullify"] = "阳伞:令一名角色本回合非锁定技失效",
  ["#emo__yangsan-move"] = "阳伞:移动场上1张牌",

  ["$emo__yangsan"] = "高贵地。",
}

return skel
